// pages/service/withdraw-money/index.js
var app = getApp(), api = require('../../../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      
  }, 
  onShow: function(){
    var that = this;
    var cash = wx.getStorageSync('cash');
    app.request({
      url: api.user.bank,
      method: 'post',
      data: { 
          cid: cash.cid,
          uid: wx.getStorageSync('uid'),
          bid: wx.getStorageSync('bid')
        },
      success: function (e) {
        if(e.code == 1){
          that.setData({
            data: e.data,
           
          })
          console.log(that.data.data)
        }else if(e.code == 0){
          wx.showModal({
            title: '提示',
            content: '未设置银行卡',
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/service/pages/service/bankcard-binding/index',
                })
              } else if (res.cancel) {
                wx.navigateTo({
                  url: '/service/pages/service/balance-management/index',
                })
              }
            }
          })  
        }else{
          wx.showToast({
            title: '网络错误！！',
            icon: 'loading',
            duration: 2000
          }) 
        }
      }
    })
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "balance") {
      that.setData({
        balance: inputVlaue
      });
    }
  },
  sendRequest: function () {
    var that = this;
    var uid = wx.getStorageSync('uid');
    var bid = wx.getStorageSync('bid');
    var money = that.data.balance;
    if (that.validate()) {
      // TODO 请求
      app.request({
        url: api.default.moneysub,
        method: 'post',
        data: {
          uid: uid,
          bid:bid,
          money: money
        },
        success: function (res) {
          console.log(res)
          if (res.code==1){
            wx.showModal({
              title: '提示',
              content: '提现申请成功',
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/service/pages/service/balance-management/index',
                  })
                } else if (res.cancel) {
                  wx.navigateTo({
                    url: '/service/pages/service/withdraw-money/index',
                  })
                }
              }
            })
          }else{
            wx.showToast({
              title: '网络错误！！',
              icon: 'loading',
              duration: 2000
            }) 
          }  
        }
      })

    }
  },
  validate: function () {
    var that = this;
    var balance = that.data.balance;
    var money = that.data.data.money;
    if (!that.data.balance || that.data.balance < 1) {
      wx.showToast({
        title: '取现金额不足1元',
        icon: 'none',
        mask: true,
      });
      return false;
    }

    if (!that.data.balance || Number(balance) > Number(money)) {
      wx.showToast({
        title: '取现余额不足',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    return true;
  }
})